import {Component, OnInit, ViewChild} from '@angular/core';
import {adSystemApiUrl} from '../../../../config/api';
import {HttpService} from '../../../lib/service/http.service';
import {ModalDirective} from 'ngx-bootstrap';

@Component({
  selector: 'app-add-attr',
  templateUrl: 'add-attr.component.html',
  styleUrls: ['add-attr.component.scss']
})
export class AddAttrComponent implements OnInit {

  type: string;
  message: string;
  isShow = false;

  classifyList = '';
  classifyId: number;
  attr: string;  // 属性名
  attrId: number;  // 属性名ID
  editAttr: string;  // 修改属性名
  attrList: any = '';  // 属性名列表
  attrValList: any = ''; // 属性值列表
  attrValId: number;  // 属性值ID
  attrVal: string;  // 属性值
  editAttrVal: string;  // 修改属性值

  @ViewChild('editModal') public editModal: ModalDirective;
  @ViewChild('editValModal') public editValModal: ModalDirective;
  @ViewChild('delModal') public delModal: ModalDirective;
  @ViewChild('delValModal') public delValModal: ModalDirective;

  constructor(private httpService: HttpService) {
  }

  ngOnInit() {
    this.getClassifyList();
  }

  // 获取分类列表
  getClassifyList() {
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute_category/list?&page=1&pageSize=9999`;
    return this.httpService.httpGetAuth(url)
      .subscribe(
        res => {
          this.classifyList = res.data.data;
        }
      );
  }

  // 获取属性名列表
  getAttrList() {
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute/list?goods_attribute_category_id=${this.classifyId}&page=1&pageSize=9999`;
    return this.httpService.httpGetAuth(url)
      .subscribe(
        res => {
          this.attrList = res.data.data;
        }
      );
  }

  // 获取属性值列表
  getAttrValList() {
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute_value/list?goods_attribute_id=${this.attrId}&page=1&pageSize=9999`;
    return this.httpService.httpGetAuth(url)
      .subscribe(
        res => {
          this.attrValList = res.data.data;
        }
      );
  }

  // 选中分类
  chosedClassify(id: number) {
    this.classifyId = id;
    this.attrId = null;
    this.attrValId = null;
    this.attrValList = [];
    this.getAttrList();
  }

  // 添加属性名
  addAttr() {
    if (!this.classifyId) {
      this.type = 'info';
      this.message = '请先选择分类';
      this.isShow = true;
      return;
    }
    if (!this.attr) {
      this.type = 'info';
      this.message = '请输入属性名';
      this.isShow = true;
      return;
    }
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute/add`;
    const data = {name: this.attr, goods_attribute_category_id: this.classifyId};
    return this.httpService.httpPostAuth(url, data)
      .subscribe(
        res => {
          if (res.code === 2000) {
            this.type = 'success';
            this.message = '添加成功';
            this.isShow = true;
            this.attr = null;
            this.getAttrList();
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      );
  }

  // 修改属性名let
  editAttrDialog(id: number, name: string, classifyId: number) {
    this.attrId = id;
    this.editAttr = name;
    this.classifyId = classifyId;
    this.editModal.show();
  }

  // 修改属性名表单提交
  editAttrSubmit() {
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute/edit`;
    const data = {id: this.attrId, name: this.editAttr, goods_attribute_category_id: this.classifyId};
    return this.httpService.httpPostAuth(url, data)
      .subscribe(
        res => {
          if (res.code === 2000) {
            this.editModal.hide();
            this.type = 'success';
            this.message = '修改成功';
            this.isShow = true;
            this.getAttrList();
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      );
  }

  // 删除属性
  delAttr(id: number) {
    this.attrId = id;
    this.delModal.show();
  }

  // 确认删除属性
  delAttrSubmit() {
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute/${this.attrId}/del`;
    return this.httpService.httpPostAuth(url, '')
      .subscribe(
        res => {
          if (res.code === 2000) {
            this.delModal.hide();
            this.type = 'success';
            this.message = '删除成功';
            this.isShow = true;
            this.getAttrList();
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      );
  }

  // 选中属性
  chosedAttr(id: number) {
    this.attrId = id;
    this.getAttrValList();
  }

  // 选中属性名
  chosedAttrVal(id: number) {
    this.attrValId = id;
  }

  // 添加属性名
  addAttrVal() {
    if (!this.attrId) {
      this.type = 'info';
      this.message = '请先选择属性';
      this.isShow = true;
      return;
    }
    if (!this.attrVal) {
      this.type = 'info';
      this.message = '请输入属性值';
      this.isShow = true;
      return;
    }
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute_value/add`;
    const data = {value: this.attrVal, goods_attribute_id: this.attrId};
    return this.httpService.httpPostAuth(url, data)
      .subscribe(
        res => {
          if (res.code === 2000) {
            this.attrVal = null;
            this.getAttrValList();
            this.type = 'success';
            this.message = '添加成功';
            this.isShow = true;
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      );
  }

  // 修改属性值
  editAttrValDialog(id: number, name: string) {
    this.attrValId = id;
    this.editAttrVal = name;
    this.editValModal.show();
  }

  // 修改属性值表单提交
  editAttrValSubmit() {
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute_value/edit`;
    const data = {id: this.attrValId, value: this.editAttrVal, goods_attribute_id: this.attrId};
    return this.httpService.httpPostAuth(url, data)
      .subscribe(
        res => {
          if (res.code === 2000) {
            this.editValModal.hide();
            this.type = 'success';
            this.message = '修改成功';
            this.isShow = true;
            this.getAttrValList();
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      );
  }

  // 删除属性值表单提交
  delAttrValSubmit() {
    const url = `${adSystemApiUrl}/v1/admin/goods_attribute_value/${this.attrValId}/del`;
    return this.httpService.httpPostAuth(url, '')
      .subscribe(
        res => {
          if (res.code === 2000) {
            this.delValModal.hide();
            this.type = 'success';
            this.message = '删除成功';
            this.isShow = true;
            this.getAttrValList();
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      );
  }

  promptIsShow(event: any): void {
    this.isShow = event;
  }

}
